package cn.zm.trip.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.zm.trip.web.commons.Msg;
import cn.zm.trip.web.dao.HotelDao;
import cn.zm.trip.web.domain.*;
import cn.zm.trip.web.service.HotelFacilityService;
import cn.zm.trip.web.service.ViewPointService;
import cn.zm.trip.web.service.WordsService;
import cn.zm.trip.web.vo.HotelVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "hotel")
public class HotelController {
	@Autowired
	private HotelDao hotelDao;
	@Autowired
	private ViewPointService viewPointService;
	@Autowired
	private WordsService wordsService;
	@Autowired
	private HotelFacilityService hotelFacilityService;

	/**
	 * 跳转首页
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(Model model, Integer page,String orderBy){
		//实例化hotel examle
		Hotel example = new Hotel();
		if (StringUtils.isBlank(orderBy)){
			example.setOrderBy("heat");
		}else {
			example.setOrderBy(orderBy);
		}
		example.setStart(page==null?0:(page-1)*4);
		List<Hotel> hotels = hotelDao.hotelPointSearch(example);
		//传送景点
		model.addAttribute("hotels", hotels);
		return "proscenium/hotel/index";
	}

	/**
	 * 跳转首页
	 */
	@RequestMapping(value = "content", method = RequestMethod.GET)
	public String content(Integer hid, Model model) {

		//封装留言信息
		List<Words> lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		List<Reply> lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

		Hotel hotel = hotelDao.selectByPrimaryKey(hid);
		HotelFacility hotelFacility = hotelFacilityService
			.getOne(new LambdaQueryWrapper<HotelFacility>().eq(HotelFacility::getHotelId, hotel.getHid()));
		List<WordStatisticsEntity> wordStatisticsEntities = wordsService.listWordStaticsByWordQuery(null,hid,null);
		if (CollUtil.isNotEmpty(wordStatisticsEntities)) {
			int sum = wordStatisticsEntities.stream().mapToInt(WordStatisticsEntity::getQuantity).sum();

			for (WordStatisticsEntity statisticsEntity : wordStatisticsEntities) {
				Integer quantity = statisticsEntity.getQuantity();
				double radio = (quantity * 1.0) / sum;
				String proportion = (int) ((radio) * 100) + "%";
				statisticsEntity.setScoreTextByScore();
				statisticsEntity.setScoreRatioText(proportion);
			}
			model.addAttribute("wordStatics",wordStatisticsEntities);
		}
		//统计人气
		Hotel updateEntity = new Hotel();
		Integer heat = hotel.getHeat();
		updateEntity.setHid(hotel.getHid());
		updateEntity.setHeat(++heat);
		hotelDao.updateById(updateEntity);

		model.addAttribute("hotel", HotelVO.buildHotelVO(hotel,hotelFacility));

		return "proscenium/hotel/content";
	}

	/**
	 * 前台酒店模糊搜索
	 */
	@RequestMapping(value = "hotelPointSearch", method = RequestMethod.GET)
	public String hotelPointSearch(String keyword, Model model) {

		Hotel hotel = new Hotel();
		hotel.setTitle(keyword);
		hotel.setLocal(keyword);
		hotel.setHouseType(keyword);
		hotel.setBedType(keyword);
		List<Hotel> hotels = hotelDao.hotelPointSearch(hotel);


		model.addAttribute("hotels", hotels);
		model.addAttribute("msg", Msg.success("酒店查询成功!"));

		return "proscenium/hotel/index";
	}


}
